package calao.client.util;

public abstract class ArrayHandler {
	
	public Object[] remove(Object[] array, Object occurence)
	{
		int count = 0;
		Object[] result = newArray(array.length);
		for (int i=0; i<array.length; i++) {
			if (array[i]!=occurence) result[count++] = array[i];
		}
		if (count==array.length) return result;
		else return copyOf(result, count);
	}
	
	public Object[] add(Object[] array, Object object)
	{
		Object[] result = copyOf(array, array.length+1);
		result[array.length] = object;
		return result;
	}

	abstract public Object[] newArray(int length);

	public Object[] copyOf(Object[] array, int newLength)
	{
		Object[] result = newArray(newLength);
		for (int i=0; i<newLength; i++) result[i] = i<array.length? array[i] : null;
		return result;
		
	}
	
	public Object[] copyOf(Object[] array)
	{
		return copyOf(array, array.length);
	}

}
